"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能-功能测试
Case Name   : 无法重复安装存在的停用插件
Create At   : 2023/5/19
Owner       : n@ningyali
Description :
    1. 打开插件管理页面
    2. 停用插件
    3. 点击右上角“安装插件”按钮
    4. 将这个停用插件的Jar包拖入安装窗口【结果1】
Expect      :
    1.【结果1】提示插件已经存在，且不影响插件正常使用
History     :
"""
import os
import time
import unittest

from selenium.webdriver.common.by import By

from openGaussMigrate.common.base import Base
from openGaussMigrate.common.com_log import ComLog
from openGaussMigrate.common.com_node import Macro
from openGaussMigrate.common.com_unit_action import ComUnitAction


class Install0008(unittest.TestCase):
    def setUp(self):
        self.log = ComLog(self.__class__.__name__)
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        web = Macro('WEB_INFO')
        self.driver = ComUnitAction(web.info['driver_path'], web.info['url'])
        self.driver.open()
        self.driver.login(web.info['user'], web.info['passwd'])
        self.log.info('setup run finished')
        self.plugin_name = "data-migration"
        self.plugin_name_cn = "数据迁移"
        self.plugin_path = os.path.join(os.getcwd(),
                                        'resources',
                                        'plugins',
                                        'data-migration-5.0.0-repackage.jar')

    @Base.retry(max_n=1)
    @Base.save_img_when_err(cls_name='Install0008')
    def test_case1(self):
        """无法重复安装存在的停用插件"""
        self.log.info('前置操作：安装插件')
        if not self.driver.is_plugin_intalled(self.plugin_name):
            self.driver.install_plugin(self.plugin_name_cn, self.plugin_path)

        self.log.info('步骤1：打开插件管理页面')
        btn_xpath = '//*[text()="插件管理" and contains(@class, "arco-menu")]'
        self.driver.wait_click(btn_xpath)

        self.log.info('步骤2：停用插件')
        btn_xpath = '//*[text()="data-migration"]/ancestor::div/descendant' \
                    '::span[text()="启用"]/preceding-sibling::button'
        if self.driver.is_found(By.XPATH, btn_xpath):
            self.driver.click(btn_xpath)
            time.sleep(3)

        self.log.info('步骤3：点击右上角“安装插件”按钮')
        btn_xpath = '//*[text()="安装插件" and contains(@class, "arco-btn")]'
        self.driver.wait_click(btn_xpath)

        self.log.info('步骤4：将这个停用插件的Jar包拖入安装窗口')
        upload_xpath = '//*[text()="点击上传"]'
        self.driver.wait_click(upload_xpath)
        time.sleep(2)
        Base.upload(self.plugin_path)
        start_xpath = '//*[@class="arco-upload-icon arco-upload-icon-start"]'
        self.driver.wait_click(start_xpath)
        time.sleep(3)

        self.log.info('步骤5：提示插件已经存在')
        hint_xpath = '//*[text()="plugin install error"]'
        self.driver.wait_ele(By.XPATH, hint_xpath)
        self.driver.refresh()

    def tearDown(self):
        try:
            if self.driver.is_plugin_intalled(self.plugin_name):
                self.driver.uninstall_plugin(self.plugin_name)
        except Exception as e:
            self.log.error(str(e))
        finally:
            self.driver.close()
            self.log.info('teardown run finished')
